import 'package:flutter/material.dart';
import 'package:flutter_layout/utils/request.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

class Case extends StatelessWidget {
  const Case({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        SizedBox(height: 100.h),
        ElevatedButton(
            onPressed: () async {
              //isGranted 授权
              //isDenied 拒绝
              // isPermanentlyDenied 永久拒绝

              final status = await Permission.camera.status;
              if (status.isDenied) {
                await Permission.camera.request();
              } else if (status.isPermanentlyDenied) {
                // 打开设置
                await openAppSettings();
              }

              final status1 = await Permission.photos.status;
              if (status1.isDenied) {
                await Permission.photos.request();
              } else if (status1.isPermanentlyDenied) {
                // 打开设置
                await openAppSettings();
              }

              final status2 = await Permission.storage.status;
              print(status2);
              if (status2.isDenied) {
                await Permission.storage.request();
              } else if (status2.isPermanentlyDenied) {
                // 打开设置
                await openAppSettings();
              }
            },
            child: Text("获取权限")),
        ElevatedButton(
            onPressed: () async {
              // https://pub.dev/packages/image_picker

              final ImagePicker picker = ImagePicker();
              final List<XFile>? images = await picker.pickMultiImage(
                limit: 9,
                imageQuality: 80, // 图片质量 0-100
              );
              final request = await Request().uploadFile(
                  data: images!
                      .map((e) => FileItem(filename: e.name, filepath: e.path))
                      .toList());

              print(request);
            },
            child: Text('获取多张图')),
        ElevatedButton(
            onPressed: () async {
              // https://pub.dev/packages/image_picker

              final ImagePicker picker = ImagePicker();
              final XFile? images = await picker.pickImage(
                source: ImageSource.gallery,
                imageQuality: 80, // 图片质量 0-100
              );
              if (images == null) return;
              final request = await Request().uploadFile(data: [
                FileItem(filepath: images.path, filename: images.name)
              ]);
            },
            child: Text('获取单张图')),
        ElevatedButton(
            onPressed: () async {
              final tempDir = await getTemporaryDirectory();
              final savePath =
                  '${tempDir.path}/${DateTime.now().millisecondsSinceEpoch}.jpg';
              await Request().downloadFile(
                  urlPath:
                      'https://leshi-1325499010.cos.ap-beijing.myqcloud.com/2025/10/22/image_picker_2F82FD51-B9FE-442F-B7F3-46F4C1C8E2EB-39278-0000020FAA6467E9_20251022134156A005.jpg',
                  savePath: savePath,
                  onReceiveProgress: (received, total) {
                    print('下载进度');
                    print(received);
                    print(total);
                  });
              // 添加错误处理并确保正确调用插件
              try {
                final result = await ImageGallerySaver.saveFile(savePath);
                print('保存结果: $result');
              } catch (e) {
                print('保存到相册失败: $e');
              }
            },
            child: Text("保存到相册"))
      ],
    );
  }
}
